package com.wooboo.server.ext.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

import net.sf.json.JSONObject;

public class Crypto_bak {
	public static final String ALGORITHM = "DES";
	public static final String KEY = "dfyfhqs9";
	
	public static String encryptData(String strs) {
		try{
			byte[] contentBytes = strs.getBytes();
	    	byte[] keyBytes = KEY.getBytes();
	        byte[] cookieBytes = encryptData(contentBytes, keyBytes);
	        return ByteHelper.byte2hex(cookieBytes);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}

	}
	public static String decryptData(String strs){
		try{
			byte[] cookieBytes = ByteHelper.hex2byte(strs);
	        byte[] decodedBytes = decryptData(cookieBytes, KEY.getBytes());
	     	String decodedString = new String(decodedBytes);
	     	return decodedString;
		}catch(Exception e){
			//e.printStackTrace();
			return null;
		}
		
	}
	private static byte[] decryptData(byte[] input, byte[] key) throws Exception { 
	    SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, ALGORITHM); 
	    Cipher c1 = Cipher.getInstance(ALGORITHM); 
	    c1.init(Cipher.DECRYPT_MODE, deskey); 
	    byte[] clearByte = c1.doFinal(input);   
	    return clearByte; 
	} 
	
    private static byte[] encryptData(byte[] input, byte[] key) throws Exception { 
        SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, ALGORITHM); 
        Cipher c1 = Cipher.getInstance(ALGORITHM); 
        c1.init(Cipher.ENCRYPT_MODE, deskey); 
        byte[] cipherByte = c1.doFinal(input);   
        return cipherByte;  
    } 
    
    
    
    public static void main(String[] args){
    	JSONObject o = new JSONObject();
    	o.put("code", "200");
    	o.put("userId", "20398");
    	o.put("id", "20");
    	o.put("name", "东方红");
    	o.put("imei", "1qsiwekcksdlsdsd");
    	String strs = encryptData(o.toString());
    	System.out.println(strs+"   "+strs.length());
    	System.out.println(decryptData(strs));
    	
    }
}
